import { Killer } from "../../funcObj/interface/HealthPoint";
import { MapLength } from "../../info/Predefine";
import GameManager  from "../../manager/GameManager";
import Player from "../../player/Player";
import DestructibleFacility from "../DestructibleFacility";
import Facility from "../Facility";

const { ccclass, property } = cc._decorator;

/**
 * 反导雷达
 */
@ccclass
export default class Radar extends DestructibleFacility {

    init(player: Player): void {
        super.init(player);
        this.player.node.on(Player.EventType.被设定为目标, this.onAttacked, this);
    }

    /**
     * 
     * @param fac 攻击我的设施
     */
    onAttacked(fac: Facility) {
        //显示攻击我方的设施 
        if (fac instanceof DestructibleFacility) {
            let facLocation = GameManager.instance.getMapLocation(fac.node.position);
            let leftUp = { locationX: Math.max(0, facLocation.locationX - 2), locationY: Math.max(0, facLocation.locationY - 2) };
            let rightDown = { locationX: Math.min(MapLength - 1, facLocation.locationX + 2), locationY: Math.min(MapLength - 1, facLocation.locationY + 2) };
            for (let i = leftUp.locationX; i <= rightDown.locationX; i++) {
                for (let j = leftUp.locationY; j <= rightDown.locationY; j++) {
                    this.player.getLocationView({ locationX: i, locationY: j })
                }
            }
        }
    }

    die(killer: Killer): void {
        super.die(killer);
        this.player.node.targetOff(this);
    }

}
